package com.sig.net.core;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpResponseInterceptor;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

import android.util.Log;

public class Net {

	public static final String OK = "OK";
	public static final String NO = "NO";

	public static String ADRESSE_SERVEUR = "localhost";
	public static final String SITE = "/WebServiceSIG/";

	public static List<NameValuePair> newListNameValuePair(){
		return new ArrayList<NameValuePair>();
	}

	public static List<NameValuePair> construireDonnes(Object...donnes){
		List<NameValuePair> donneesPost = new ArrayList<NameValuePair>();
		add(donneesPost,donnes);
		return donneesPost;
	}

	public static void add(List<NameValuePair> donneesPost, Object...donnes){
		for(int i=0;i<donnes.length;i+=2){
			String cle = donnes[i].toString();
			String valeur = donnes[i+1].toString();
			donneesPost.add(new BasicNameValuePair(cle, valeur));
		}
	}
	
	public static String requeteGet(String url, List<NameValuePair> donnees){

		if(donnees == null)
			donnees = Net.construireDonnes();

		StringBuilder sb = new StringBuilder();
		if(!url.contains("?"))
			sb.append("?");
		if(donnees != null){
			boolean premier = true;
			for(NameValuePair nvp : donnees){
				if(!premier)
					sb.append("&");
				premier = false;

				sb.append(nvp.getName());
				sb.append("=");
				sb.append(nvp.getValue());
			}
		}
		
		String urlRequete = "http://"+ADRESSE_SERVEUR+SITE+url+sb.toString().trim();
		Log.d("NET_GET",urlRequete);

		HttpClient httpClient = new DefaultHttpClient();
		try {

			HttpGet requete = new HttpGet(urlRequete);

			HttpResponse httpReponse = httpClient.execute(requete);

			StatusLine statusLine = httpReponse.getStatusLine();
			Log.e("NET_ST",statusLine.toString());
			if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
				Log.d("NET_STATUS","STATUS OK");
			}else
				Log.d("NET_STATUS","STATUS PAS OK");
			
			//String response = responseToString(httpReponse.getEntity());
			String response = EntityUtils.toString( httpReponse.getEntity(), HTTP.ISO_8859_1 ).trim().replace("&aecute", "??");  
			Log.d("NET",response);
			Log.d("NET_REPONSE_TAILLE",""+response.length());
			return response;
			//}

		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();

		} finally{
			if(httpClient!=null)
				httpClient.getConnectionManager().shutdown();
		}

		Log.e("NET","echec de la requete");
		return NO; //en cas d'echec
	}

	public static String responseToString(HttpEntity entity){
		StringBuilder out = new StringBuilder();

		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(entity.getContent()));
			for(String line = reader.readLine(); line != null; line = reader.readLine()) 
				out.append(line);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally{
			if(reader != null)
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}

		return out.toString();
	}

}
